home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Development
/
Source
/
Watch Volume Mount
/
launcher.cc
next >
Wrap
Text File
|
1994-02-10
|
4KB
|
128 lines
/*
***********************************************************************
*
* This is a simple program that, given a file specification, launches
* an application-creator and has it handle the file. The net result is
* exactly the same as if the user had double-clicked on the file.
*
* Synopsis
* void open_selection(const FSSpec& file_spec)
* Where the 'file_spec' tells the file specification (volume refno,
* directory ID and the sile name) of the file to "double-click" on.
*
* The present program achieves the magic by sending an 'Open Selection'
* event to the Finder. It is significantly based on the FinderEvents
* stack by Jon Pugh and Apple Computer, Inc. (© 1991-92 Apple Computer, Inc.)
*
* OpenSelection event requires two parameters:
* - alias of the Folder the file resides in
* - list containing an alias of the file(s) to "double-click" on
*
*
***********************************************************************
*/
/* MacHeaders Included */
#include "myenv.h"
#include <string.h>
#include <stdarg.h>
#include <AppleEvents.h>
#include <Aliases.h>
// Create an 'open selection' event to be sent to Finder
static AppleEvent create_event_for_finder(void)
{
const AEEventClass kAEFinderEvents = 'FNDR'; // Finder event
const AEEventID kOpenSelection = 'sope'; // OpenSelection event
const OSType kFinderSignature = 'MACS'; // Our destination
AppleEvent the_event;
AEAddressDesc finder_id;
do_well( AECreateDesc(typeApplSignature,(void *)&kFinderSignature,sizeof(kFinderSignature),
&finder_id) );
do_well( AECreateAppleEvent(kAEFinderEvents,kOpenSelection,&finder_id,
kAutoGenerateReturnID,
kAnyTransactionID,
&the_event) );
do_well( AEDisposeDesc(&finder_id) ); // the_event is created, finder_id can
// be disposed of now
return the_event;
}
// Add the alias of file or path name to the event
// Only the path fieled is considered
static void add_path_name(AppleEvent * the_event_ptr, const FSSpec& file_spec)
{
AliasHandle alias;
FSSpec dir_spec;
do_well( FSMakeFSSpec(file_spec.vRefNum,file_spec.parID,"\p",&dir_spec) );
do_well( NewAliasMinimal(&dir_spec,&alias) );
HLock((char **)alias);
do_well( AEPutParamPtr(the_event_ptr, keyDirectObject, typeAlias, StripAddress(*alias),
(**alias).aliasSize) );
HUnlock((char **)alias);
DisposHandle((char **)alias);
}
// Create a "selection" item in the event from the
// full file name specified
static void add_selection(AppleEvent * the_event_ptr, const FSSpec& file_spec)
{
AliasHandle alias;
AEDescList selection_list;
do_well( NewAliasMinimal(&file_spec,&alias) );
do_well( AECreateList(nil, 0, FALSE, &selection_list) );
HLock((char **)alias);
do_well( AEPutPtr(&selection_list, 1, typeAlias, StripAddress(*alias),
(**alias).aliasSize) );
HUnlock((char **)alias);
DisposHandle((char **)alias);
do_well( AEPutParamDesc(the_event_ptr, 'fsel', &selection_list) );
do_well( AEDeleteItem(&selection_list, 1) );
assert( AEDisposeDesc(&selection_list) == noErr );
}
//-------------------------------------------------------------------------
// Routing module
//
void open_selection(const FSSpec& file_spec)
{
char path_name[255]; // Path to the file, ends in :
char * p;
AppleEvent the_event, reply;
#if 0
// Getting just a path name from the full
// file name
assure(strlen(full_path_name) < sizeof(path_name),
"Full path name is too big");
if( (p=strrchr(full_path_name,':')) == (char *)0 )
path_name[0] = '\0'; // Path name is empty
else
strncpy(path_name,full_path_name,p-full_path_name+1);
#endif
the_event = create_event_for_finder();
add_path_name(&the_event,file_spec);
add_selection(&the_event,file_spec);
do_well( AESend(&the_event, &reply, kAENoReply+kAENeverInteract,
kAENormalPriority, kAEDefaultTimeout, nil, nil) );
do_well( AEDisposeDesc(&the_event) );
do_well( AEDisposeDesc(&reply) );
}